package fafu.comedu.workerrepair;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.TextView;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import fafu.comedu.workerrepair.command.Dictionary;
import fafu.comedu.workerrepair.command.LocalDataStream;
import fafu.comedu.workerrepair.fragment.HumanInfoFragement;
import fafu.comedu.workerrepair.fragment.HumanInfoChange;
import fafu.comedu.workerrepair.fragment.Human_pwd_change;
import fafu.comedu.workerrepair.fragment.RecordFragment;
import fafu.comedu.workerrepair.fragment.RecordItemFragment;

public class MainActivity extends AppCompatActivity {
    private Fragment fragments[];
    private int lastfragment;//用于记录上个选择的Fragment
    private TextView mTextMessage;
    private Human_pwd_change human_pwd_change;
    private HumanInfoFragement humanInfoFragement;
    private RecordFragment runnningFragment;
    private RecordFragment historyFragment;
    private RecordItemFragment recordItemFragment;
    private HumanInfoChange humanInfoChange;
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.menu_running:
                    if(lastfragment!=0){ //进行
                        switchFragment(lastfragment,0);
                        lastfragment=0;
                    }
                    return true;
                case R.id.menu_history:
                    if(lastfragment!=1){ //历史
                        switchFragment(lastfragment,1);
                        lastfragment=1;
                    }
                    return true;
                case R.id.menu_human_info:
                    if(lastfragment!=2){ //历史
                        switchFragment(lastfragment,2);
                        lastfragment=2;
                    }
                    return true;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        initFragment();

    }

    private void initFragment() {
        runnningFragment = new RecordFragment("run");//未读，未处理，处理中
        historyFragment = new RecordFragment("his");//历史
        humanInfoFragement = new HumanInfoFragement();
        human_pwd_change = new Human_pwd_change();
        recordItemFragment = new RecordItemFragment();
        humanInfoChange = new HumanInfoChange();
        fragments = new Fragment[]{runnningFragment,historyFragment,humanInfoFragement,human_pwd_change,recordItemFragment,humanInfoChange};
//        fragments = new Fragment[]{test_fragment,historyFragment,applyFragement,refuseFragment,humanInfoFragement};
        getSupportFragmentManager().beginTransaction().replace(R.id.mainview,fragments[lastfragment]).show(fragments[lastfragment]).commit();
    }

    public int getLastfragment() {
        return lastfragment;
    }

    public void setRecordId(Integer recordId){
        ((RecordItemFragment)fragments[Dictionary.Page.FRAGMENT_RECORD_ITEM]).setRecordId(recordId);
    }

    private void logout(){
        Log.i("logout", "logout: 123");
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("是否确认退出");

        builder.setPositiveButton("取消", null);
        builder.setNeutralButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                try {
                    FileOutputStream out = openFileOutput("data", Context.MODE_PRIVATE);
                    LocalDataStream.writer("",out);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

                //清空本地账号信息；
                Intent intent = new Intent(MainActivity.this,LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
        WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
        layoutParams.width = 700;
        dialog.getWindow().setAttributes(layoutParams);
    }
    public void setLastfragment(int lastfragment) {
        this.lastfragment = lastfragment;
    }
    //切换Fragment
    public void switchFragment(int lastfragment,int index){
        FragmentTransaction transaction =getSupportFragmentManager().beginTransaction();
        transaction.hide(fragments[lastfragment]);//隐藏上个Fragment
        if(fragments[index].isAdded()==false){
            transaction.add(R.id.mainview,fragments[index]);
        }
        transaction.show(fragments[index]).commitAllowingStateLoss();
    }
}
